import { Controller } from "../../mvc/core/Controller";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { PetExplorePackWindow } from "../view/pet/explore/PetExplorePackWindow";
import { SPetExploreCards } from "../../../Message/Public/SPetExploreCards";
import { PetExploreConfigInfo } from "../resource/info/PetExploreConfigInfo";
import { SPetExploreInfo } from "../../../Message/Public/SPetExploreInfo";
import { SActiveToClient } from "../../../Message/Public/SActiveToClient";
import { EActiveType } from "../../../Message/Public/EActiveType";
import { ClockManager } from "../manager/ClockManager";
import { MergeRechargeData } from "../view/consumeCount/MergeRechargeData";
import { PetExploreAgainWindow } from "../view/pet/explore/PetExploreAgainWindow";
import { IView } from "../../mvc/interfaces/IView";
import { PetExploreWindow } from "../view/pet/explore/PetExploreWindow";
import { PetExploreBattleWindow } from "../view/pet/explore/PetExploreBattleWindow";
import { PetExploreAreaInfo } from "../resource/info/PetExploreAreaInfo";
import { PetExploreEnableAreaWindow } from "../view/pet/explore/PetExploreEnableAreaWindow";
import { PetExploreSkipWindow } from "../view/pet/explore/PetExploreSkipWindow";
import { PetExploreRewardWindow } from "../view/pet/explore/PetExploreRewardWindow";
import { PetExploreConfig } from "../resource/PetExploreConfig";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { EPlayerItemPosType } from "../../../Message/Public/EPlayerItemPosType";
import { SPetExploreResult } from "../../../Message/Public/SPetExploreResult";
import { PetExploreSuccessWindow } from "../view/pet/explore/PetExploreSuccessWindow";
import { PulseSharedObject } from "../view/player/PulseSharedObject";
import { PetExploreFailureWindow } from "../view/pet/explore/PetExploreFailureWindow";
import { ThingUtil } from "../scene/layer/utils/ThingUtil";
import { MsgTipInfo } from "../model/MsgTipInfo";
import { PetExploreResultTipPanel } from "../view/pet/explore/PetExploreResultTipPanel";
type int = number;
//class PetExploreController
    
    export  class PetExploreController extends Controller
    {
        constructor()
        {
            
            super();Dispatcher.addEventListener(EventName.PetExploreBattleWindowOpen, this.onPetExploreBattleWindowOpenHandler);
            Dispatcher.addEventListener(EventName.PetExploreBattleWindowClose, this.onPetExploreBattleWindowCloseHandler);
            Dispatcher.addEventListener(EventName.PetExploreOpen, this.onPetExploreOpenHandler);
            Dispatcher.addEventListener(EventName.PetExploreEnableWindowOpen, this.onPetExploreEnableWindowOpenHandler);
            Dispatcher.addEventListener(EventName.PetExploreSkipWindowOpen, this.onPetExploreSkipWindowOpenHandler);
            Dispatcher.addEventListener(EventName.PetExploreGetRewardWindowOpen, this.onPetExploreGetRewardWindowOpenHandler);
            Dispatcher.addEventListener(EventName.PetExplorePackWindowOpen, this.onPetExplorePackWindowOpenHandler);
            Dispatcher.addEventListener(EventName.PetExploreSetOrder, this.onPetExploreSetOrderHandler);
            Dispatcher.addEventListener(EventName.PetExploreStart, this.onPetExploreStartHandler);
            Dispatcher.addEventListener(EventName.PetExploreStartReponse, this.onPetExploreStartReponseHandler);
            Dispatcher.addEventListener(EventName.PetExploreOpenArea, this.onPetExploreOpenAreaHandler);
            Dispatcher.addEventListener(EventName.PetExploreOpenAreaSuccess, this.onPetExploreOpenAreaSuccessHandler);
            Dispatcher.addEventListener(EventName.PetExploreGetItem, this.onPetExploreGetItemHandler);
            Dispatcher.addEventListener(EventName.PetExploreSetOrderSuccess, this.onPetExploreSetOrderSuccessHandler);
            Dispatcher.addEventListener(EventName.PetExploreWatch, this.onPetExploreWatchHandler);
            Dispatcher.addEventListener(EventName.PetExploreWatchSuccess, this.onPetExploreWatchSuccessHandler);
            Dispatcher.addEventListener(EventName.PetExploreGetReward, this.onPetExploreGetRewardHandler);
            Dispatcher.addEventListener(EventName.PetExploreGetRewardSuccess, this.onPetExploreGetRewardSuccessHandler);
            Dispatcher.addEventListener(EventName.PetExploreUseCard, this.onPetExploreUseCardHandler);
            Dispatcher.addEventListener(EventName.PetExploreAgainWindowOpen, this.onPetExploreAgainWindowOpenHandler);
            Dispatcher.addEventListener(EventName.PetExploreAgainStart, this.onPetExploreAgainStartHandler);
            Dispatcher.addEventListener(EventName.PetExploreAutoStop, this.onPetExploreAutoStopHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetExploreResultUpdate, this.onPetExploreResultUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetExploreCardsCommand, this.onPetExploreCardsCommandHandler);
            NetDispatcher.addCmdListener(ServerCommand.EActiveTypePetAutoExploreMoreIncome, this.onAcitveTimeUpdateHandler);
            return;
        }

        /* internal  */onPetExploreWatchSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Boolean;
            if (loc1) 
            {
                this.onPetExploreBattleWindowOpenHandler(null);
            }
            return;
        }

        /* internal  */onPetExploreGetRewardHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Array<any>;
            GameProxy.petProxy.drawPetExploreRewards(loc1);
            return;
        }

        /* internal  */onPetExploreGetRewardSuccessHandler(arg1: DataEvent): void
        {
            if (this._rewardWindow) 
            {
                this._rewardWindow.hide();
            }
            if (this._petExploreWindow) 
            {
                this._petExploreWindow.resetGetRewardsBtn();
            }
            return;
        }

        /* internal  */onPetExploreUseCardHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["uid"] as string;
            var loc2=arg1.data["isDestroy"] as Boolean;
            GameProxy.petProxy.usePetExploreCard(loc1, loc2);
            return;
        }

        /* internal  */onPetExplorePackWindowOpenHandler(arg1: DataEvent): void
        {
            if (this._packWindow == null) 
            {
                this._packWindow = new PetExplorePackWindow();
            }
            this._packWindow.show();
            return;
        }

        /* internal  */onPetExploreCardsCommandHandler(arg1: SPetExploreCards): void
        {
            if (this._petExploreWindow && this._petExploreWindow.isHide == false) 
            {
                this._petExploreWindow.updateCardsPanel(arg1);
            }
            if (this._battleWindow && this._battleWindow.isHide == false) 
            {
                this._battleWindow.updateCardsPanel(arg1);
            }
            return;
        }

        /* internal  */onPetExploreAgainStartHandler(arg1: DataEvent): void
        {
            var e: DataEvent;
            var configInfo: PetExploreConfigInfo;
            var petExploreInfo: SPetExploreInfo;

            var loc1;
            configInfo = null;
            petExploreInfo = null;
            e = arg1;
            configInfo = e.data as PetExploreConfigInfo;
            if (configInfo) 
            {
                petExploreInfo = cache.petExplore.petExploreInfo;
                GameProxy.petProxy.addPetExploreMaxTimes(petExploreInfo.maxTimes + 1, (): void
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.PetExploreStart, {"startLevel": configInfo.level, "isAutoExplore": false, "isDrawRewards": false, "callBack": petExploreAgainStartReponse}));
                    return;
                })
            }
            return;
        }

        /* internal  */petExploreAgainStartReponse(): void
        {
            this.exploreAgainWindow.hide();
            Dispatcher.dispatchEvent(new DataEvent(EventName.PetExploreBattleWindowOpen));
            cache.petExplore.initJoinPet();
            return;
        }

        /* internal  */onPetExploreAutoStopHandler(arg1: DataEvent): void
        {
            GameProxy.petProxy.pauseAutoPetExplore(arg1.data as Function);
            return;
        }

        /* internal  */onAcitveTimeUpdateHandler(arg1: SActiveToClient): void
        {
            cache.petExplore.isActive = cache.active.isActiveInEffect(EActiveType._EActiveTypePetAutoExploreMoreIncome);
            if (cache.petExplore.isActive) 
            {
                ClockManager.instance.addDateCall(arg1.endDt, this.removeMergeRechargeActive);
            }
            return;
        }

        /* internal  */removeMergeRechargeActive(): void
        {
            if (MergeRechargeData.activeToClient) 
            {
                cache.petExplore.isActive = false;
                ClockManager.instance.removeDateCall(MergeRechargeData.activeToClient.endDt);
            }
            return;
        }

        public get exploreAgainWindow(): PetExploreAgainWindow
        {
            if (this._exploreAgainWindow == null) 
            {
                this._exploreAgainWindow = new PetExploreAgainWindow();
            }
            return this._exploreAgainWindow;
        }

        protected /* override */ initServer(): void
        {
            return;
        }

        protected /* override */ initView(): IView
        {
            if (null == this._petExploreWindow) 
            {
                this._petExploreWindow = new PetExploreWindow();
                this._petExploreWindow.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onAddedToStageHandler);
            }
            return this._petExploreWindow;
        }

        public /* override */ get view(): IView
        {
            return this.initView();
        }

        /* internal  */onAddedToStageHandler(arg1: Object /* flash.events.Event */): void
        {
            return;
        }

        /* internal  */onPetExploreBattleWindowOpenHandler(arg1: DataEvent): void
        {
            if (this._battleWindow == null) 
            {
                this._battleWindow = new PetExploreBattleWindow();
            }
            this._battleWindow.show();
            if (this._petExploreWindow) 
            {
                this._petExploreWindow.hide();
            }
            return;
        }

        /* internal  */onPetExploreBattleWindowCloseHandler(arg1: DataEvent): void
        {
            if (this._battleWindow) 
            {
                this._battleWindow.hide();
            }
            return;
        }

        /* internal  */onPetExploreOpenHandler(arg1: DataEvent): void
        {
            if (this._battleWindow && this._battleWindow.isHide == false) 
            {
                return;
            }
            var loc1=arg1.data as Boolean;
            if (this._petExploreWindow == null) 
            {
                this._petExploreWindow = new PetExploreWindow();
            }
            this._petExploreWindow.show();
            if (loc1) 
            {
                if (this._exploredResult) 
                {
                    if (this._exploredResult.isWin) 
                    {
                        if (this._successWindow) 
                        {
                            this._successWindow.show();
                        }
                    }
                    else if (this._failureWindow) 
                    {
                        this._failureWindow.show();
                    }
                }
            }
            else 
            {
                if (this._successWindow) 
                {
                    this._successWindow.hide();
                }
                if (this._failureWindow) 
                {
                    this._failureWindow.hide();
                }
            }
            return;
        }

        /* internal  */onPetExploreEnableWindowOpenHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["cArea"] as PetExploreAreaInfo;
            var loc2=arg1.data["pArea"] as PetExploreAreaInfo;
            if (this._enableAreaWindow == null) 
            {
                this._enableAreaWindow = new PetExploreEnableAreaWindow();
            }
            this._enableAreaWindow.updateByAreaInfo(loc1, loc2);
            this._enableAreaWindow.show();
            return;
        }

        /* internal  */onPetExploreSkipWindowOpenHandler(arg1: DataEvent): void
        {
            if (this._skipWindow == null) 
            {
                this._skipWindow = new PetExploreSkipWindow();
            }
            this._skipWindow.show();
            return;
        }

        /* internal  */onPetExploreGetRewardWindowOpenHandler(arg1: DataEvent): void
        {
            if (this._rewardWindow == null) 
            {
                this._rewardWindow = new PetExploreRewardWindow(this._petExploreWindow);
            }
            this._rewardWindow.show();
            return;
        }

        /* internal  */onPetExploreAgainWindowOpenHandler(arg1: DataEvent): void
        {
            this.exploreAgainWindow.updateInfo();
            this.exploreAgainWindow.show();
            return;
        }

        /* internal  */onPetExploreSetOrderHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["uids"] as Array<any>;
            var loc2=arg1.data["isShowTip"] as Boolean;
            GameProxy.petProxy.setPetExploreOrder(loc1, loc2);
            return;
        }

        /* internal  */onPetExploreStartHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["startLevel"] as int;
            var loc2=arg1.data["isAutoExplore"] as Boolean;
            var loc3=arg1.data["isDrawRewards"] as Boolean;
            var loc4=arg1.data["callBack"] as Function;
            var loc5=arg1.data["multiple"] ? arg1.data["multiple"] as int : 1;
            GameProxy.petProxy.startPetExplore(loc1, loc2, loc3, loc5, loc4);
            return;
        }

        /* internal  */onPetExploreStartReponseHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            var loc2=PetExploreConfig.instance.getExploreConfigByLevel(loc1);
            if (loc2) 
            {
                cache.petExplore.selectedConfigInfo = loc2;
                MsgManager.addTipText(Language.getString(54538) + loc2.name);
                if (this._battleWindow) 
                {
                    this._battleWindow.updateByPetExploreConfigInfo(loc2);
                }
                cache.petExplore.mapId = loc2.map;
            }
            return;
        }

        /* internal  */onPetExploreOpenAreaHandler(arg1: DataEvent): void
        {
            GameProxy.petProxy.openPetExploreArea();
            return;
        }

        /* internal  */onPetExploreOpenAreaSuccessHandler(arg1: DataEvent): void
        {
            MsgManager.showRollTipsMsg(Language.getString(54539));
            if (this._enableAreaWindow) 
            {
                this._enableAreaWindow.hide();
            }
            return;
        }

        /* internal  */onPetExploreGetItemHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Map<any, any> /* flash.utils.Dictionary */;
            if (loc1 != null) 
            {
                GameProxy.packProxy.moveBag(EPlayerItemPosType._EPlayerItemPosTypePetExplore, EPlayerItemPosType._EPlayerItemPosTypeBag, loc1);
            }
            else 
            {
                GameProxy.packProxy.moveBag(EPlayerItemPosType._EPlayerItemPosTypePetExplore, EPlayerItemPosType._EPlayerItemPosTypeBag);
            }
            return;
        }

        /* internal  */onPetExploreResultUpdateHandler(arg1: SPetExploreResult): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=null;
            var loc4=null;
            this._exploredResult = arg1;
            if (arg1.autoExplore) 
            {
                if (cache.petExplore.isAutoExplore()) 
                {
                    cache.petExplore.autoResultArray.push(arg1);
                    if (this._petExploreWindow) 
                    {
                        this._petExploreWindow.updateAutoResult(cache.petExplore.autoResultArray);
                    }
                    if (cache.petExplore.petExploreInfo.isInExplore) 
                    {
                        flash.utils.setTimeout(this.addHistoryTip, 1000, arg1.currentLevel + 1);
                    }
                }
                else 
                {
                    if (this._petExploreWindow) 
                    {
                        this._petExploreWindow.closeAutoResultWindow();
                    }
                    cache.petExplore.autoResultArray = [];
                    this.showExploreTip(arg1);
                }
                if (arg1.isWin) 
                {
                    loc1 = PetExploreConfig.instance.getExploreConfigByLevel(arg1.currentLevel + 1);
                    if (loc1 == null || loc1 && loc1.area > cache.petExplore.getExploreAreaIndex()) 
                    {
                        cache.petExplore.selectedConfigInfo = null;
                    }
                }
            }
            else 
            {
                if (this._petExploreWindow) 
                {
                    loc2 = this._petExploreWindow;
                }
                if (this._battleWindow && this._battleWindow.isHide == false) 
                {
                    loc2 = this._battleWindow;
                }
                if (arg1.isWin) 
                {
                    if (this._successWindow == null) 
                    {
                        this._successWindow = new PetExploreSuccessWindow(loc2);
                    }
                    this._successWindow.layer = loc2;
                    this._successWindow.updateByPetExploreResult(arg1);
                    if (typeof loc2 === "petexplorebattlewindow") 
                    {
                        (this._petExploreWindow as PetExploreWindow).removePopup(this._successWindow);
                        this._successWindow.hide();
                    }
                    this._successWindow.show();
                    if (loc2.isHide == true) 
                    {
                        if (!PulseSharedObject.isTodayNotTips("petExploreResult")) 
                        {
                            this.showExploreTip(arg1);
                        }
                    }
                }
                else if (cache.petExplore.isAgainExplore() && cache.petExplore.isCanExploreToday()) 
                {
                    this.exploreAgainWindow.updateInfo();
                    this.exploreAgainWindow.show();
                }
                else 
                {
                    if (this._failureWindow == null) 
                    {
                        this._failureWindow = new PetExploreFailureWindow(loc2);
                    }
                    this._failureWindow.layer = loc2;
                    this._failureWindow.updateData();
                    this._failureWindow.show();
                    if (this._successWindow) 
                    {
                        this._successWindow.hide();
                    }
                    if (loc2.isHide == true) 
                    {
                        this.showExploreTip(arg1);
                    }
                }
                if (this._battleWindow) 
                {
                    this._battleWindow.showCardPanel(false);
                }
                loc3 = cache.petExplore.petExploreInfo.petUids;
                if ((loc4 = this._petExploreWindow.getToBattlePetUids()).length != loc3.length) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.PetExploreSetOrder, {"uids": loc4, "isShowTip": false}));
                }
                ThingUtil.sceneEntityUtil.removeEntityBySceneId(cache.petExplore.mapId);
            }
            return;
        }

        /* internal  */addHistoryTip(arg1: int): void
        {
            var loc1=PetExploreConfig.instance.getExploreConfigByLevel(arg1);
            if (loc1) 
            {
                MsgManager.addTipText(Language.getString(54538) + loc1.name);
            }
            return;
        }

        /* internal  */showExploreTip(arg1: SPetExploreResult): void
        {
            var loc1=new MsgTipInfo();
            if (!PulseSharedObject.isTodayNotTips("petExploreResult")) 
            {
                if (this._resultTipPanel == null) 
                {
                    this._resultTipPanel = new PetExploreResultTipPanel();
                }
                this._resultTipPanel.updateByExploreResult(arg1);
                loc1.displayObject = this._resultTipPanel;
                loc1.type = 1;
                loc1.notTipEvent = "petExploreResult";
                Dispatcher.dispatchEvent(new DataEvent(EventName.MsgTipDateChange, loc1));
            }
            return;
        }

        /* internal  */onPetExploreSetOrderSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Boolean;
            if (loc1) 
            {
                MsgManager.showRollTipsMsg(Language.getString(54540));
            }
            if (this._petExploreWindow) 
            {
                this._petExploreWindow.showSelectPanel(false);
            }
            return;
        }

        /* internal  */onPetExploreWatchHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Boolean;
            GameProxy.petProxy.watchPetExplore(loc1);
            return;
        }

        private /* var */_petExploreWindow: PetExploreWindow;

        private /* var */_battleWindow: PetExploreBattleWindow;

        private /* var */_successWindow: PetExploreSuccessWindow;

        private /* var */_failureWindow: PetExploreFailureWindow;

        private /* var */_enableAreaWindow: PetExploreEnableAreaWindow;

        private /* var */_skipWindow: PetExploreSkipWindow;

        private /* var */_rewardWindow: PetExploreRewardWindow;

        private /* var */_resultTipPanel: PetExploreResultTipPanel;

        private /* var */_exploreAgainWindow: PetExploreAgainWindow;

        private /* var */_exploredResult: SPetExploreResult;

        private /* var */_packWindow: PetExplorePackWindow;
    }
